package com.xh.jf_blog.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xh.jf_blog.bean.Comment;
import com.xh.jf_blog.common.sensitive.SensitiveWordFilter;
import com.xh.jf_blog.common.util.R;
import com.xh.jf_blog.mapper.CommentMapper;
import com.xh.jf_blog.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @Author PangJunjie
 * @Date 2024/3/5/005
 */
@Service
@Transactional
public class CommentServiceImpl implements CommentService {
    @Autowired
    private CommentMapper commentMapper;

    @Override
    public R getPage(int page, int limit, Comment comment) {
        PageHelper.startPage(page, limit);
        List<Comment> list = commentMapper.getAll(comment);
        PageInfo<Comment> pageInfo = new PageInfo<>(list);
        List<Comment> commentList = pageInfo.getList();
        SensitiveWordFilter.loadWordFromFile("D:\\记录\\OneDrive\\学校资料\\徐海学院\\2023-2024-1\\javaWeb大实训\\20级实训\\考研批\\最终项目\\项目代码\\jf_blog\\src\\main\\resources\\sensitive\\sensitive-word-list.txt");
        for (Comment c : commentList) {
            c.setContent(SensitiveWordFilter.Filter(c.getContent()));
        }
        return R.page(pageInfo.getTotal(), commentList);
    }

    @Override
    public void saveOrUpdate(Comment comment) {
        if (comment.getCommentId() == null) {
            comment.setCommentTime(new Date());
            commentMapper.insertSelective(comment);
        } else {
            commentMapper.updateByPrimaryKeySelective(comment);
        }
    }

    @Override
    public void deleteById(int commentId) {
        commentMapper.deleteByPrimaryKey(commentId);
    }
}
